import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
//        m1();
//        m2();
//        m3();
//        m4();
        m5();
    }

    private static void m1() {
        //jdbc操作数据库
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //1、 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "6666");
//            if (connection!=null){
//                System.out.println("连接成功");
//            }
            //3.sql
            String sql = "select * from user";
            //4.预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //5.执行sql
            resultSet = preparedStatement.executeQuery();
            //6.遍历结果
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id + "----" + name + "----" + pass);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //7.关闭资源:从后往前依次关闭
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //查询
    private static void m2() {
        //键盘录入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的用户名：");
        String name = scanner.next();
        //jdbc操作数据库
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //1、 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "6666");
//            if (connection!=null){
//                System.out.println("连接成功");
//            }
            //3.定义sql查询语句
            String sql = "select * from user where name like ? ";
            //4.预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //5.设置参数
            preparedStatement.setString(1,"%"+name+"%");
            //6.执行sql
            resultSet = preparedStatement.executeQuery();
            //7.遍历结果
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id + "----" + name + "----" + pass);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //8.关闭资源:从后往前依次关闭
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //增加
    private static void m3() {
        //jdbc操作数据库
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //1、 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "6666");
            //3.定义sql插入语句
            String sql = "insert into user (id ,name ,pass) values(?,?,?)";
            //4.预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //5设置sql语句的参数
            preparedStatement.setInt(1,5);
            preparedStatement.setString(2,"小明");
            preparedStatement.setString(3,"123456");
//            if (connection!=null){
//                System.out.println("连接成功");
//            }
            //6.执行插入操作
            int result = preparedStatement.executeUpdate();
            if (result>0){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //7.关闭资源:从后往前依次关闭
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //修改
    private static void m4() {
        //jdbc操作数据库
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //1、 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "6666");
            //3.定义sql修改语句
            String sql = "update user set name=?,pass=? where id= ?";
            //4.预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //5.设置参数
            preparedStatement.setString(1,"wang");
            preparedStatement.setString(2,"222");
            preparedStatement.setInt(3,5);
//            if (connection!=null){
//                System.out.println("连接成功");
//            }
            //6.执行修改操作
            int result = preparedStatement.executeUpdate();
            if (result>0){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //7.关闭资源:从后往前依次关闭
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //删除
    private static void m5() {
        //jdbc操作数据库
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //1加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "6666");
            //3.定义sql删除语句
            String sql = "delete from user where id=?";
            //4.预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //5设置参数
            preparedStatement.setInt(1,1);
            //preparedStatement.setObject(1,5); 因为Objext是所有类的父类，所以无论是哪种类型的都可以直接使用这个
//            if (connection!=null){
//                System.out.println("连接成功");
//            }
            //6.执行删除操作
            int result = preparedStatement.executeUpdate();
            if (result>0){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //7.关闭资源:从后往前依次关闭
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}